﻿using CsQuery;
using DDLStorageLinks.Command;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace DDLStorageLinks.ViewModel
{
    class DDLStorageViewModel : INotifyPropertyChanged
    {
        private List<String> links;

        private static readonly String rootUri = "http://www.ddlstorage.com/folder/";
        private String folderUri = DDLStorageViewModel.rootUri;

        private SynchronizationContext syncContext = SynchronizationContext.Current;

        public ICommand CopyCommand { get; private set; }
        public ICommand CancelCommand { get; private set; }
        public ICommand GetCommand { get; private set; }

        public DDLStorageViewModel()
        {
            CopyCommand = new MvvmCommand((arg) =>
            {
                StringBuilder linkBuilder = new StringBuilder();

                foreach (String link in Links)
                {
                    linkBuilder.AppendLine(link);
                }

                Clipboard.SetText(linkBuilder.ToString());
            }, (arg) =>
            {
                return Links != null && Links.Count > 0;
            })
            .AddListener<DDLStorageViewModel>(this, x => x.Links);

            CancelCommand = new MvvmCommand((arg) =>
            {
                if (Links == null)
                {
                    return;
                }

                Links.Clear();
                Links = new List<string>();
            }, (arg) =>
            {
                return Links != null && Links.Count > 0;
            })
            .AddListener<DDLStorageViewModel>(this, x => x.Links);

            GetCommand = new MvvmCommand((arg) =>
            {
                Task.Factory.StartNew(() =>
                    {
                        CQ dom = CQ.CreateFromUrl(FolderUri);
                        CQ links = dom["#file_list tbody a"];

                        var state = (from l in links
                                     select l["href"])
                                    .ToList();

                        syncContext.Post(s => Links = (List<String>)s, state);
                    });
            }, (arg) =>
            {
                return FolderUri.Contains(DDLStorageViewModel.rootUri) &&
                    FolderUri.Length > DDLStorageViewModel.rootUri.Length;
            })
            .AddListener<DDLStorageViewModel>(this, x => x.FolderUri);
        }

        public List<String> Links
        {
            get
            {                
                return links;
            }
            set
            {
                links = value;
                OnPropertyChanged("Links");
            }
        }

        public String FolderUri
        {
            get
            {
                return folderUri;
            }
            set
            {
                folderUri = value;
                OnPropertyChanged("FolderUri");
            }
        }

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                this.PropertyChanged(this, e);
            }
        }
        #endregion
    }
}
